package chatserver.container;

import chatserver.objects.User;
import java.util.ArrayList;
import java.util.Iterator;


public class AliasInfo {
    
    private String ip;
    private ArrayList<User> userAliases;
    private int connectionCount;
    
    public AliasInfo(String ip)
    {
        this.ip = ip;
        this.connectionCount = 1;
        this.userAliases = new ArrayList<User>();
    }
    
    public void increase()
    {
       connectionCount++;
    }
    
    public void decrease()
    {
        connectionCount--;
    }
    
    public int getConnectionCount()
    {
        return connectionCount;
    }
    
    public void addAlias(User u)
    {
        userAliases.add(u);
    }

    public void removeAlias(User u)
    {
        if(userAliases.contains(u))
        {
            userAliases.remove(u);
        }
    }    
    
    public String getAliasesOfUser(User ofU)
    {
        String ret = "";
        boolean first = true;
        Iterator<User> iter = userAliases.iterator();
        while(iter.hasNext())
        {
            User u = iter.next();
            if(u.equals(ofU))
            {
                continue;
            }
            if(first)
            {
                ret = u.getName();
                first = false;
            }
            else
            {
                ret += ", " + u.getName();
            }
        }
        
        return ret;
    }
    
}
